package com.lucun.insurance.service.Impl;

import com.lucun.insurance.db.entity.InsureEntryFamily;
import com.lucun.insurance.db.mapper.InsureEntryChildMapper;
import com.lucun.insurance.db.mapper.InsureEntryFamilyMapper;
import com.lucun.insurance.service.InsureEntryFamilyService;
import com.lucun.insurance.util.BaseException;
import com.lucun.insurance.util.JsonResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 郭鑫
 * @catalogue: com.lucun.insurance.service.Impl
 * @Date: 2021/9/9 17:13
 * @Version: 1.0
 */
@Service
public class InsureEntryFamilyServiceImpl implements InsureEntryFamilyService {
    @Autowired
    InsureEntryFamilyMapper insureEntryFamilyMapper;

    //    查询所有
    @Override
    public List<InsureEntryFamily> findAll() {
        return insureEntryFamilyMapper.findAll();
    }

    //添加
    @Override
    public JsonResult<?> insert(InsureEntryFamily insureEntryFamily) {
        if (ObjectUtils.isEmpty(insureEntryFamily)) {
            throw new BaseException("添加数据不能为空");
        }
        String name = insureEntryFamily.getName();
        if (StringUtils.isBlank(name)) {
            throw new BaseException("录入类型不能为空");
        }
        String company = insureEntryFamily.getCompany();
        if (StringUtils.isBlank(company)) {
            throw new BaseException("单位名称不能为空");
        }
        String insuredAddress = insureEntryFamily.getInsuredAddress();
        if (StringUtils.isBlank(insuredAddress)) {
            throw new BaseException("受保人地址不能为空");
        }
        String laborNumber = insureEntryFamily.getLaborNumber();
        if (StringUtils.isBlank(laborNumber)){
            throw new BaseException("劳保证号不能为空");
        }
        String staffName = insureEntryFamily.getStaffName();
        if (StringUtils.isBlank(staffName)){
            throw new BaseException("员工姓名不能为空");
        }
        Byte directRelationship = insureEntryFamily.getDirectRelationship();
        if (ObjectUtils.isEmpty(directRelationship)){
            throw new BaseException("直系关系不能为空");
        }
        Long userId = insureEntryFamily.getUserId();
        if (ObjectUtils.isEmpty(userId)) {
            throw new BaseException("投保人Id不能为空");
        }
        Long insureId = insureEntryFamily.getInsureId();
        if (ObjectUtils.isEmpty(insureId)) {
            throw new BaseException("被保人id不能为空");
        }
        InsureEntryFamily w = insureEntryFamilyMapper.selectById(insureEntryFamily.getInsureId());
        if (!ObjectUtils.isEmpty(w)) {
            throw new BaseException("该员工已添加录入类型员工类型");
        }
        int i = insureEntryFamilyMapper.insert(insureEntryFamily);
        if (i != 0) {
            return new JsonResult<>("200");
        }
        return new JsonResult<>("500");
    }

    //修改
    @Override
    public JsonResult<?> update(InsureEntryFamily insureEntryFamily) {
        if (ObjectUtils.isEmpty(insureEntryFamily)) {
            throw new BaseException("没有获取数据");
        }
        int i = insureEntryFamilyMapper.updateByPrimaryKeySelective(insureEntryFamily);
        if (i != 0) {
            return new JsonResult<>("200");
        }
        return new JsonResult<>("500");
    }

    //    根据被保人Id删除
    @Override
    public JsonResult<?> delete(InsureEntryFamily insureEntryFamily) {
        Long id = insureEntryFamily.getInsureId();
        if (ObjectUtils.isEmpty(id)) {
            throw new BaseException("没有获取此id");
        }
        int i = insureEntryFamilyMapper.deleteById(id);
        if (i != 0) {
            return new JsonResult<>("200");
        }
        return new JsonResult<>("500");
    }
}